﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using System.Web.Mvc;
using CommonSearchModule;
using CommonSearchModule.com.webservice.www;

namespace Lucene测试.Controllers
{
    [HandleError]
    public class HomeController : ControllerBase
    {
        Web.Helper.UIHelper ui;


        public ActionResult Index(string keyword, string categoryName, string gid, string sid, int? page)
        {
            #region URL参数格式化及索引处理
            string _keyword = (keyword ?? string.Empty).Trim();
            string _categoryName = (categoryName ?? string.Empty).Trim();
            string _propertyName = GroupPropertyName(gid, sid);

            System.Collections.Specialized.NameValueCollection nv = new System.Collections.Specialized.NameValueCollection();
            nv.Add("keyword", _keyword);
            nv.Add("categoryName", _categoryName);
            nv.Add("propertyName", _propertyName);
            ui = new Web.Helper.UIHelper(Request.Url.ToString(), nv);
            SearchResult result = GetDataFromIndex(new QueryTerm { KeyWord = _keyword, CategoryName = _categoryName, PropertyName = _propertyName, PageSize = 1 });


            #endregion


            ViewData["result"] = result;
            ViewData["page"] = ui.GetPage(page ?? 1, 10, result.Total);
            ViewData["Message"] = "欢迎使用 ASP.NET MVC!";

            return View();
        }

        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            #region Form参数格式化及索引处理
            string _keyword = (form["keyword"] ?? string.Empty).Trim();
            string _categoryName = (form["categoryName"] ?? string.Empty).Trim();
            string _propertyName = GroupPropertyName(form["gid"], form["sid"]);

            System.Collections.Specialized.NameValueCollection nv = new System.Collections.Specialized.NameValueCollection();
            nv.Add("keyword", _keyword);
            nv.Add("categoryName", _categoryName);
            nv.Add("propertyName", _propertyName);
            ui = new Web.Helper.UIHelper(Request.Url.ToString(), nv);
            SearchResult result = GetDataFromIndex(new QueryTerm { KeyWord = _keyword, CategoryName = _categoryName, PropertyName = _propertyName, PageSize = 1 });
            #endregion

            ViewData["result"] = result;
            ViewData["page"] = ui.GetPage(1, 10, result.Total);
            ViewData["Message"] = "欢迎使用 ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            ViewData["Message"] = "欢迎使用 ASP.NET MVC!";

            return View();
        }
    }
}